<?php
/**
 *   Joomdiet - administrator/components/controllers/recipe.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');

class JoomdietControllerRecipe extends JController
{
    function __construct()
    {
        // get requested view
        if (JRequest::getCmd('view') == '') {
            JRequest::setVar('view', 'default');
        }
        $this->item_type = 'Default';
        parent::__construct();
        
        $this->registerTask('addRecipe', 'editRecipe');
    }
    
    function deleteRecipe()
    {
        $model = $this->getModel('recipe');
        $cid = JRequest::getVar( 'cid', array(0), 'post', 'array' );
        
        if (!is_array($cid) || count($cid) < 1) {
            $msg = '';
			JError::raiseWarning(500, JText::_( 'COM_JOOMDIET_SELECT_DELETE' ) );
        } else {
            foreach ($cid as $id) {
                if (!$model->delete($id)) {
                    JError::raiseError(500, JText::_('COM_JOOMDIET_DELETE_FAILED').'<pre>'.print_r($cid, true).'</pre>');
                }
            }
            
            $msg = count($cid).' '.JText::_( 'COM_JOOMDIET_SELECTED_DELETE' );
			$cache = &JFactory::getCache('com_joomdiet');
			$cache->clean();
        }
        
        $this->setRedirect( 'index.php?option=com_joomdiet&view=recipe', $msg );
    }
    
    function editRecipe()
    {
        JRequest::setVar('view', 'editrecipe');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('recipe');
        $user =& JFactory::getUser();
        
        $view =& $this->getView('EditRecipe', 'html');
        $view->setModel($this->getModel('Recipe'), true);
        $view->setModel($this->getModel('RecipeCategories'));
        $view->setModel($this->getModel('Food'));
        $view->setModel($this->getModel('RecipeFood'));
        
        $view->display();
    }
    
    function saveRecipe()
    {
        JRequest::setVar('view', 'editrecipe');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('recipe');
        $user =& JFactory::getUser();
        
        // updating model data
        $id = $model->store();
        JRequest::setVar('cid', array($id));
        
        // updating recipe food model data
        $recipefood_model = $this->getModel('RecipeFood');
        $recipefood_model->apply($id);
        
        $link 	= 'index.php?option=com_joomdiet&view=recipe';
        $this->setRedirect($link, JText::_('COM_JOOMDIET_RECIPE_SAVED'));
    }
    
    function addfood()
    {
        JRequest::setVar('view', 'editrecipe');
        JRequest::setVar('hidemainmenu', 1);
        $model = $this->getModel('recipe');
        $user =& JFactory::getUser();
        
        // updating model data
        $id = $model->store();
        JRequest::setVar('cid', array($id));
        
        // adding food
        $recipefood_model = $this->getModel('RecipeFood');
        $recipe_id = JRequest::getVar('recipe_id');
        $food_id = JRequest::getVar('foodselector');
        $recipefood_model->insert($recipe_id, $food_id);
        
        // reloading view
        $view =& $this->getView('EditRecipe', 'html');
        $view->setModel($this->getModel('Recipe'), true);
        $view->setModel($this->getModel('RecipeCategories'));
        $view->setModel($this->getModel('Food'));
        $view->setModel($this->getModel('RecipeFood'));
        
        $view->display();
    }
    
    function deletefood()
    {
        JRequest::setVar('view', 'editrecipe');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('recipe');
        $user =& JFactory::getUser();
        
        // updating model data
        $id = $model->store();
        JRequest::setVar('cid', array($id));
        
        // delete food
        $recipefood_model = $this->getModel('RecipeFood');
        $rid = JRequest::getVar('rid');
        $fid = JRequest::getVar('fid');
        $recipefood_model->delete($rid, $fid);
        
        // reloading view
        $view =& $this->getView('EditRecipe', 'html');
        $view->setModel($this->getModel('Recipe'), true);
        $view->setModel($this->getModel('RecipeCategories'));
        $view->setModel($this->getModel('Food'));
        $view->setModel($this->getModel('RecipeFood'));
        
        $view->display();
    }
    
    function applyrecipe()
    {
        JRequest::setVar('view', 'editrecipe');
        JRequest::setVar('hidemainmenu', 1);
        
        $model = $this->getModel('recipe');
        $user =& JFactory::getUser();
        
        // updating model data
        $id = $model->store();
        JRequest::setVar('cid', array($id));
        
        // updating recipe food model data
        $recipefood_model = $this->getModel('RecipeFood');
        $recipefood_model->apply($id);
        
        // reloading view
        $view =& $this->getView('EditRecipe', 'html');
        $view->setModel($this->getModel('Recipe'), true);
        $view->setModel($this->getModel('RecipeCategories'));
        $view->setModel($this->getModel('Food'));
        $view->setModel($this->getModel('RecipeFood'));
        
        $view->display();
    }
}
?>
